package com.demo.pattern.singleton;

/**
 * @author sourire
 * @version 1.0
 * @date 2020/8/17 16:53
 */
public class SingletonTest02 {
    public static void main(String[] args) {
        SingleTon02 instance = SingleTon02.getInstance();
        SingleTon02 instance2 = SingleTon02.getInstance();
        System.out.println(instance == instance2);
        System.out.println("instance === " + instance.hashCode());
        System.out.println("instance2 === " + instance2.hashCode());
    }
}

//静态代码块饿汉式

class SingleTon02 {
    //构造器私有化
    private SingleTon02() {
    }
    //在类的内部创建实例
    private  static SingleTon02 instance ;

    //在静态代码块创建实例
    static {
        instance = new SingleTon02();
    }
    //对外提供静态的调用方法
    public static SingleTon02 getInstance() {
        return instance;
    }
}
